
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <title>路由 · RabbitMQ in Chinese</title>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.0.0-pre.14">
        <meta name="author" content="Ping <yuanta11@gmil.com>">
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-expandable-chapters-interactive/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search-plus/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="[5]Topics.html" />
    
    
    <link rel="prev" href="[3]Publish_Subscribe.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    
    
        
        <li>
            <a href="http://mr-ping.com" target="_blank" class="custom-link">瓶先生</a>
        </li>
    
    

    
    <li class="divider"></li>
    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    RabbitMQ中文文档
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" >
            
                <span>
            
                    
                    RabbitMQ简介
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../description.html">
            
                <a href="../description.html">
            
                    
                    RabbitMQ能为你做些什么？
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" >
            
                <span>
            
                    
                    安装
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../installation/Installing_on_Debian_Ubuntu.html">
            
                <a href="../installation/Installing_on_Debian_Ubuntu.html">
            
                    
                    在Debian及Ubuntu系统上进行安装
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../installation/Platforms_supported_by_RabbitMQ.html">
            
                <a href="../installation/Platforms_supported_by_RabbitMQ.html">
            
                    
                    RabbitMQ所支持的平台
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" >
            
                <span>
            
                    
                    AMQP协议
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../AMQP/AMQP_0-9-1_Model_Explained.html">
            
                <a href="../AMQP/AMQP_0-9-1_Model_Explained.html">
            
                    
                    AMQP 0.9.1 模型解析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../AMQP/amqp-0-9-1-quickref.html">
            
                <a href="../AMQP/amqp-0-9-1-quickref.html">
            
                    
                    AMQP 0.9.1 快速参考指南
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" >
            
                <span>
            
                    
                    应用教程
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" >
            
                <span>
            
                    
                    Python版
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1.1" data-path="../tutorials_with_python/[1]Hello_World.html">
            
                <a href="../tutorials_with_python/[1]Hello_World.html">
            
                    
                    Hello World
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.1.2" data-path="../tutorials_with_python/[2]Work_Queues.html">
            
                <a href="../tutorials_with_python/[2]Work_Queues.html">
            
                    
                    工作队列
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.1.3" data-path="../tutorials_with_python/[3]Publish_Subscribe.html">
            
                <a href="../tutorials_with_python/[3]Publish_Subscribe.html">
            
                    
                    发布/订阅
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.1.4" data-path="../tutorials_with_python/[4]Routing.html">
            
                <a href="../tutorials_with_python/[4]Routing.html">
            
                    
                    路由
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.1.5" data-path="../tutorials_with_python/[5]Topics.html">
            
                <a href="../tutorials_with_python/[5]Topics.html">
            
                    
                    主题交换机
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.1.6" data-path="../tutorials_with_python/[6]RPC.html">
            
                <a href="../tutorials_with_python/[6]RPC.html">
            
                    
                    远程过程调用
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5.2" >
            
                <span>
            
                    
                    C#版
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.2.1" data-path="../tutorials_with_csharp/HelloWorld.html">
            
                <a href="../tutorials_with_csharp/HelloWorld.html">
            
                    
                    Hello World
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2.2" data-path="../tutorials_with_csharp/WorkQueue.html">
            
                <a href="../tutorials_with_csharp/WorkQueue.html">
            
                    
                    工作队列
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2.3" data-path="../tutorials_with_csharp/publish&subscribe.html">
            
                <a href="../tutorials_with_csharp/publish&subscribe.html">
            
                    
                    发布/订阅
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2.4" data-path="../tutorials_with_csharp/routing.html">
            
                <a href="../tutorials_with_csharp/routing.html">
            
                    
                    路由
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2.5" data-path="../tutorials_with_csharp/Topics.html">
            
                <a href="../tutorials_with_csharp/Topics.html">
            
                    
                    主题交换机
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2.6" data-path="../tutorials_with_csharp/rpc.html">
            
                <a href="../tutorials_with_csharp/rpc.html">
            
                    
                    远程过程调用
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5.3" >
            
                <span>
            
                    
                    golang版
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.3.1" data-path="[1]Hello_World.html">
            
                <a href="[1]Hello_World.html">
            
                    
                    Hello World
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3.2" data-path="[2]Work_Queues.html">
            
                <a href="[2]Work_Queues.html">
            
                    
                    工作队列
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3.3" data-path="[3]Publish_Subscribe.html">
            
                <a href="[3]Publish_Subscribe.html">
            
                    
                    发布/订阅
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.5.3.4" data-path="[4]Routing.html">
            
                <a href="[4]Routing.html">
            
                    
                    路由
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3.5" data-path="[5]Topics.html">
            
                <a href="[5]Topics.html">
            
                    
                    主题交换机
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3.6" data-path="[6]RPC.html">
            
                <a href="[6]RPC.html">
            
                    
                    远程过程调用
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >路由</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div class="search-plus" id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <blockquote>
<p>&#x539F;&#x6587;&#xFF1A;<a href="https://www.rabbitmq.com/tutorials/tutorial-four-go.html" _target="blank">Routing</a><br>&#x72B6;&#x6001;&#xFF1A;&#x5F85;&#x6821;&#x5BF9;<br>&#x7FFB;&#x8BD1;&#xFF1A;<a href="https://bingjian-zhu.github.io/" _target="blank">Bingjian-Zhu</a><br>&#x6821;&#x5BF9;&#xFF1A;</p>
</blockquote>
<p><img src="https://upload.wikimedia.org/wikipedia/commons/d/d0/CC-BY-SA_icon.svg" alt="CC-BY-SA"></p>
<h2 id="&#x8DEF;&#x7531;routing">&#x8DEF;&#x7531;(Routing)</h2>
<p><strong>&#xFF08;&#x4F7F;&#x7528;Go&#x5BA2;&#x6237;&#x7AEF;&#xFF09;</strong></p>
<!--more-->
<p>&#x5728;&#x524D;&#x9762;&#x7684;&#x6559;&#x7A0B;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x5B9E;&#x73B0;&#x4E86;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#x65E5;&#x5FD7;&#x7CFB;&#x7EDF;&#x3002;&#x53EF;&#x4EE5;&#x628A;&#x65E5;&#x5FD7;&#x6D88;&#x606F;&#x5E7F;&#x64AD;&#x7ED9;&#x591A;&#x4E2A;&#x63A5;&#x6536;&#x8005;&#x3002;</p>
<p>&#x672C;&#x7BC7;&#x6559;&#x7A0B;&#x4E2D;&#x6211;&#x4EEC;&#x6253;&#x7B97;&#x65B0;&#x589E;&#x4E00;&#x4E2A;&#x529F;&#x80FD; &#x2014;&#x2014; &#x4F7F;&#x5F97;&#x5B83;&#x80FD;&#x591F;&#x53EA;&#x8BA2;&#x9605;&#x6D88;&#x606F;&#x7684;&#x4E00;&#x4E2A;&#x5B57;&#x96C6;&#x3002;&#x4F8B;&#x5982;&#xFF0C;&#x6211;&#x4EEC;&#x53EA;&#x9700;&#x8981;&#x628A;&#x4E25;&#x91CD;&#x7684;&#x9519;&#x8BEF;&#x65E5;&#x5FD7;&#x4FE1;&#x606F;&#x5199;&#x5165;&#x65E5;&#x5FD7;&#x6587;&#x4EF6;&#xFF08;&#x5B58;&#x50A8;&#x5230;&#x78C1;&#x76D8;&#xFF09;&#xFF0C;&#x4F46;&#x540C;&#x65F6;&#x4ECD;&#x7136;&#x628A;&#x6240;&#x6709;&#x7684;&#x65E5;&#x5FD7;&#x4FE1;&#x606F;&#x8F93;&#x51FA;&#x5230;&#x63A7;&#x5236;&#x53F0;&#x4E2D;</p>
<h2 id="&#x7ED1;&#x5B9A;&#xFF08;bindings&#xFF09;">&#x7ED1;&#x5B9A;&#xFF08;Bindings&#xFF09;</h2>
<p>&#x524D;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#xFF0C;&#x6211;&#x4EEC;&#x5DF2;&#x7ECF;&#x521B;&#x5EFA;&#x8FC7;&#x7ED1;&#x5B9A;&#xFF08;bindings&#xFF09;&#xFF0C;&#x4EE3;&#x7801;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-go">err = ch.QueueBind(
  q.Name, <span class="hljs-comment">// queue name</span>
  <span class="hljs-string">&quot;&quot;</span>,     <span class="hljs-comment">// routing key</span>
  <span class="hljs-string">&quot;logs&quot;</span>, <span class="hljs-comment">// exchange</span>
  <span class="hljs-literal">false</span>,
  <span class="hljs-literal">nil</span>)
</code></pre>
<p>&#x7ED1;&#x5B9A;&#xFF08;binding&#xFF09;&#x662F;&#x6307;&#x4EA4;&#x6362;&#x673A;&#xFF08;exchange&#xFF09;&#x548C;&#x961F;&#x5217;&#xFF08;queue&#xFF09;&#x7684;&#x5173;&#x7CFB;&#x3002;&#x53EF;&#x4EE5;&#x7B80;&#x5355;&#x7406;&#x89E3;&#x4E3A;&#xFF1A;&#x8FD9;&#x4E2A;&#x961F;&#x5217;&#xFF08;queue&#xFF09;&#x5BF9;&#x8FD9;&#x4E2A;&#x4EA4;&#x6362;&#x673A;&#xFF08;exchange&#xFF09;&#x7684;&#x6D88;&#x606F;&#x611F;&#x5174;&#x8DA3;&#x3002;</p>
<p>&#x7ED1;&#x5B9A;&#x7684;&#x65F6;&#x5019;&#x53EF;&#x4EE5;&#x5E26;&#x4E0A;&#x4E00;&#x4E2A;&#x989D;&#x5916;&#x7684;<code>routing_key</code>&#x53C2;&#x6570;&#x3002;&#x4E3A;&#x4E86;&#x907F;&#x514D;&#x4E0E;<code>Channel.Publish</code>&#x7684;&#x53C2;&#x6570;&#x6DF7;&#x6DC6;&#xFF0C;&#x6211;&#x4EEC;&#x628A;&#x5B83;&#x53EB;&#x505A;&#x7ED1;&#x5B9A;&#x952E;<code>binding key</code>&#x3002;&#x4EE5;&#x4E0B;&#x662F;&#x5982;&#x4F55;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x5E26;&#x7ED1;&#x5B9A;&#x952E;&#x7684;&#x7ED1;&#x5B9A;&#x3002;</p>
<pre><code class="lang-go">err = ch.QueueBind(
  q.Name,    <span class="hljs-comment">// queue name</span>
  <span class="hljs-string">&quot;black&quot;</span>,   <span class="hljs-comment">// routing key</span>
  <span class="hljs-string">&quot;logs&quot;</span>,    <span class="hljs-comment">// exchange</span>
  <span class="hljs-literal">false</span>,
  <span class="hljs-literal">nil</span>)
</code></pre>
<p>&#x7ED1;&#x5B9A;&#x952E;&#x7684;&#x610F;&#x4E49;&#x53D6;&#x51B3;&#x4E8E;&#x4EA4;&#x6362;&#x673A;&#xFF08;exchange&#xFF09;&#x7684;&#x7C7B;&#x578B;&#x3002;&#x6211;&#x4EEC;&#x4E4B;&#x524D;&#x4F7F;&#x7528;&#x8FC7;<code>fanout</code> &#x4EA4;&#x6362;&#x673A;&#x4F1A;&#x5FFD;&#x7565;&#x8FD9;&#x4E2A;&#x503C;&#x3002;</p>
<h2 id="&#x76F4;&#x8FDE;&#x4EA4;&#x6362;&#x673A;&#xFF08;direct-exchange&#xFF09;">&#x76F4;&#x8FDE;&#x4EA4;&#x6362;&#x673A;&#xFF08;Direct exchange&#xFF09;</h2>
<p>&#x6211;&#x4EEC;&#x7684;&#x65E5;&#x5FD7;&#x7CFB;&#x7EDF;&#x5E7F;&#x64AD;&#x6240;&#x6709;&#x7684;&#x6D88;&#x606F;&#x7ED9;&#x6240;&#x6709;&#x7684;&#x6D88;&#x8D39;&#x8005;&#xFF08;consumers&#xFF09;&#x3002;&#x6211;&#x4EEC;&#x6253;&#x7B97;&#x6269;&#x5C55;&#x5B83;&#xFF0C;&#x4F7F;&#x5176;&#x57FA;&#x4E8E;&#x65E5;&#x5FD7;&#x7684;&#x4E25;&#x91CD;&#x7A0B;&#x5EA6;&#x8FDB;&#x884C;&#x6D88;&#x606F;&#x8FC7;&#x6EE4;&#x3002;&#x4F8B;&#x5982;&#x6211;&#x4EEC;&#x4E5F;&#x8BB8;&#x53EA;&#x662F;&#x5E0C;&#x671B;&#x5C06;&#x6BD4;&#x8F83;&#x4E25;&#x91CD;&#x7684;&#x9519;&#x8BEF;&#xFF08;error&#xFF09;&#x65E5;&#x5FD7;&#x5199;&#x5165;&#x78C1;&#x76D8;&#xFF0C;&#x4EE5;&#x514D;&#x5728;&#x8B66;&#x544A;&#xFF08;warning&#xFF09;&#x6216;&#x8005;&#x4FE1;&#x606F;&#xFF08;info&#xFF09;&#x65E5;&#x5FD7;&#x4E0A;&#x6D6A;&#x8D39;&#x78C1;&#x76D8;&#x7A7A;&#x95F4;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x4F7F;&#x7528;&#x7684;<code>fanout</code> &#x4EA4;&#x6362;&#x673A;&#x6CA1;&#x6709;&#x8DB3;&#x591F;&#x7684;&#x7075;&#x6D3B;&#x6027; &#x2014;&#x2014; &#x5B83;&#x80FD;&#x505A;&#x7684;&#x4EC5;&#x4EC5;&#x662F;&#x5E7F;&#x64AD;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x5C06;&#x4F1A;&#x4F7F;&#x7528;<code>direct</code> &#x4EA4;&#x6362;&#x673A;&#x6765;&#x4EE3;&#x66FF;&#x3002;&#x8DEF;&#x7531;&#x7684;&#x7B97;&#x6CD5;&#x5F88;&#x7B80;&#x5355; &#x2014;&#x2014; &#x4EA4;&#x6362;&#x673A;&#x5C06;&#x4F1A;&#x5BF9;<code>binding key</code>&#x548C;<code>routing key</code>&#x8FDB;&#x884C;&#x7CBE;&#x786E;&#x5339;&#x914D;&#xFF0C;&#x4ECE;&#x800C;&#x786E;&#x5B9A;&#x6D88;&#x606F;&#x8BE5;&#x5206;&#x53D1;&#x5230;&#x54EA;&#x4E2A;&#x961F;&#x5217;&#x3002;</p>
<p>&#x4E0B;&#x56FE;&#x80FD;&#x591F;&#x5F88;&#x597D;&#x7684;&#x63CF;&#x8FF0;&#x8FD9;&#x4E2A;&#x573A;&#x666F;&#xFF1A;</p>
<p><img src="http://www.rabbitmq.com/img/tutorials/direct-exchange.png" alt=""></p>
<p>&#x5728;&#x8FD9;&#x4E2A;&#x573A;&#x666F;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x770B;&#x5230;<code>direct</code>&#x4EA4;&#x6362;&#x673A; X&#x548C;&#x4E24;&#x4E2A;&#x961F;&#x5217;&#x8FDB;&#x884C;&#x4E86;&#x7ED1;&#x5B9A;&#x3002;&#x7B2C;&#x4E00;&#x4E2A;&#x961F;&#x5217;&#x4F7F;&#x7528;<code>orange</code>&#x4F5C;&#x4E3A;binding key&#xFF0C;&#x7B2C;&#x4E8C;&#x4E2A;&#x961F;&#x5217;&#x6709;&#x4E24;&#x4E2A;&#x7ED1;&#x5B9A;&#xFF0C;&#x4E00;&#x4E2A;&#x4F7F;&#x7528;<code>black</code>&#x4F5C;&#x4E3A;binding key&#xFF0C;&#x53E6;&#x5916;&#x4E00;&#x4E2A;&#x4F7F;&#x7528;<code>green</code>&#x3002;</p>
<p>&#x8FD9;&#x6837;&#x4EE5;&#x6765;&#xFF0C;&#x5F53;&#x6D88;&#x606F;&#x53D1;&#x5E03;&#x5230;routing key&#x4E3A;<code>orange</code>&#x7684;&#x4EA4;&#x6362;&#x673A;&#x65F6;&#xFF0C;&#x5C31;&#x4F1A;&#x88AB;&#x8DEF;&#x7531;&#x5230;&#x961F;&#x5217;Q1&#x3002;routing key&#x4E3A;<code>black</code>&#x6216;&#x8005;<code>green</code>&#x7684;&#x6D88;&#x606F;&#x5C31;&#x4F1A;&#x8DEF;&#x7531;&#x5230;Q2&#x3002;&#x5176;&#x4ED6;&#x7684;&#x6240;&#x6709;&#x6D88;&#x606F;&#x90FD;&#x5C06;&#x4F1A;&#x88AB;&#x4E22;&#x5F03;&#x3002;</p>
<h2 id="&#x591A;&#x4E2A;&#x7ED1;&#x5B9A;&#xFF08;multiple-bindings&#xFF09;">&#x591A;&#x4E2A;&#x7ED1;&#x5B9A;&#xFF08;Multiple bindings&#xFF09;</h2>
<p><img src="http://www.rabbitmq.com/img/tutorials/direct-exchange-multiple.png" alt=""></p>
<p>&#x591A;&#x4E2A;&#x961F;&#x5217;&#x4F7F;&#x7528;&#x76F8;&#x540C;&#x7684;binding key&#x662F;&#x5408;&#x6CD5;&#x7684;&#x3002;&#x8FD9;&#x4E2A;&#x4F8B;&#x5B50;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x6DFB;&#x52A0;&#x4E00;&#x4E2A;X&#x548C;Q1&#x4E4B;&#x95F4;&#x7684;&#x7ED1;&#x5B9A;&#xFF0C;&#x4F7F;&#x7528;<code>black</code>&#x4E3A;binding key&#x3002;&#x8FD9;&#x6837;&#x4E00;&#x6765;&#xFF0C;<code>direct</code>&#x4EA4;&#x6362;&#x673A;&#x5C31;&#x548C;<code>fanout</code>&#x4EA4;&#x6362;&#x673A;&#x7684;&#x884C;&#x4E3A;&#x4E00;&#x6837;&#xFF0C;&#x4F1A;&#x5C06;&#x6D88;&#x606F;&#x5E7F;&#x64AD;&#x5230;&#x6240;&#x6709;&#x5339;&#x914D;&#x7684;&#x961F;&#x5217;&#x3002;&#x5E26;&#x6709;routing key&#x4E3A;<code>black</code>&#x7684;&#x6D88;&#x606F;&#x4F1A;&#x540C;&#x65F6;&#x53D1;&#x9001;&#x5230;Q1&#x548C;Q2&#x3002;</p>
<h2 id="&#x53D1;&#x9001;&#x65E5;&#x5FD7;">&#x53D1;&#x9001;&#x65E5;&#x5FD7;</h2>
<p>&#x6211;&#x4EEC;&#x5C06;&#x4F1A;&#x53D1;&#x9001;&#x6D88;&#x606F;&#x5230;&#x4E00;&#x4E2A;<code>fanout</code>&#xFF0C;&#x628A;&#x65E5;&#x5FD7;&#x7B49;&#x7EA7;&#x4F5C;&#x4E3A;<code>routing key</code>&#x3002;&#x8FD9;&#x6837;&#x63A5;&#x6536;&#x65E5;&#x5FD7;&#x5C31;&#x53EF;&#x4EE5;&#x6839;&#x636E;&#x65E5;&#x5FD7;&#x7EA7;&#x522B;&#x6765;&#x9009;&#x62E9;&#x5B83;&#x60F3;&#x8981;&#x5904;&#x7406;&#x7684;&#x65E5;&#x5FD7;&#x3002;&#x6211;&#x4EEC;&#x5148;&#x770B;&#x770B;&#x53D1;&#x9001;&#x65E5;&#x5FD7;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x9700;&#x8981;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x4EA4;&#x6362;&#x673A;&#xFF08;exchange&#xFF09;&#xFF1A;</p>
<pre><code class="lang-go">err = ch.ExchangeDeclare(
  <span class="hljs-string">&quot;logs_direct&quot;</span>, <span class="hljs-comment">// name</span>
  <span class="hljs-string">&quot;direct&quot;</span>,      <span class="hljs-comment">// type</span>
  <span class="hljs-literal">true</span>,          <span class="hljs-comment">// durable</span>
  <span class="hljs-literal">false</span>,         <span class="hljs-comment">// auto-deleted</span>
  <span class="hljs-literal">false</span>,         <span class="hljs-comment">// internal</span>
  <span class="hljs-literal">false</span>,         <span class="hljs-comment">// no-wait</span>
  <span class="hljs-literal">nil</span>,           <span class="hljs-comment">// arguments</span>
)
</code></pre>
<p>&#x7136;&#x540E;&#x6211;&#x4EEC;&#x53D1;&#x9001;&#x4E00;&#x5219;&#x6D88;&#x606F;&#xFF1A;</p>
<pre><code class="lang-go">err = ch.ExchangeDeclare(
  <span class="hljs-string">&quot;logs_direct&quot;</span>, <span class="hljs-comment">// name</span>
  <span class="hljs-string">&quot;direct&quot;</span>,      <span class="hljs-comment">// type</span>
  <span class="hljs-literal">true</span>,          <span class="hljs-comment">// durable</span>
  <span class="hljs-literal">false</span>,         <span class="hljs-comment">// auto-deleted</span>
  <span class="hljs-literal">false</span>,         <span class="hljs-comment">// internal</span>
  <span class="hljs-literal">false</span>,         <span class="hljs-comment">// no-wait</span>
  <span class="hljs-literal">nil</span>,           <span class="hljs-comment">// arguments</span>
)
failOnError(err, <span class="hljs-string">&quot;Failed to declare an exchange&quot;</span>)

body := bodyFrom(os.Args)
err = ch.Publish(
  <span class="hljs-string">&quot;logs_direct&quot;</span>,         <span class="hljs-comment">// exchange</span>
  severityFrom(os.Args), <span class="hljs-comment">// routing key</span>
  <span class="hljs-literal">false</span>, <span class="hljs-comment">// mandatory</span>
  <span class="hljs-literal">false</span>, <span class="hljs-comment">// immediate</span>
  amqp.Publishing{
    ContentType: <span class="hljs-string">&quot;text/plain&quot;</span>,
    Body:        []<span class="hljs-keyword">byte</span>(body),
})
</code></pre>
<p>&#x6211;&#x4EEC;&#x5047;&#x8BBE;&#x65E5;&#x5FD7;&#x7B49;&#x7EA7;&#x7684;&#x503C;&#x662F;info&#x3001;warning&#x3001;error&#x4E2D;&#x7684;&#x4E00;&#x4E2A;&#x3002;</p>
<h2 id="&#x8BA2;&#x9605;">&#x8BA2;&#x9605;</h2>
<p>&#x5904;&#x7406;&#x63A5;&#x6536;&#x6D88;&#x606F;&#x7684;&#x65B9;&#x5F0F;&#x548C;&#x4E4B;&#x524D;&#x5DEE;&#x4E0D;&#x591A;&#xFF0C;&#x53EA;&#x6709;&#x4E00;&#x4E2A;&#x4F8B;&#x5916;&#xFF0C;&#x6211;&#x4EEC;&#x5C06;&#x4F1A;&#x4E3A;&#x6211;&#x4EEC;&#x611F;&#x5174;&#x8DA3;&#x7684;&#x6BCF;&#x4E2A;&#x4E25;&#x91CD;&#x7EA7;&#x522B;&#x5206;&#x522B;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x7ED1;&#x5B9A;&#x3002;</p>
<pre><code class="lang-go">q, err := ch.QueueDeclare(
  <span class="hljs-string">&quot;&quot;</span>,    <span class="hljs-comment">// name</span>
  <span class="hljs-literal">false</span>, <span class="hljs-comment">// durable</span>
  <span class="hljs-literal">false</span>, <span class="hljs-comment">// delete when usused</span>
  <span class="hljs-literal">true</span>,  <span class="hljs-comment">// exclusive</span>
  <span class="hljs-literal">false</span>, <span class="hljs-comment">// no-wait</span>
  <span class="hljs-literal">nil</span>,   <span class="hljs-comment">// arguments</span>
)
failOnError(err, <span class="hljs-string">&quot;Failed to declare a queue&quot;</span>)

<span class="hljs-keyword">if</span> <span class="hljs-built_in">len</span>(os.Args) &lt; <span class="hljs-number">2</span> {
  log.Printf(<span class="hljs-string">&quot;Usage: %s [info] [warning] [error]&quot;</span>, os.Args[<span class="hljs-number">0</span>])
  os.Exit(<span class="hljs-number">0</span>)
}
<span class="hljs-keyword">for</span> _, s := <span class="hljs-keyword">range</span> os.Args[<span class="hljs-number">1</span>:] {
  log.Printf(<span class="hljs-string">&quot;Binding queue %s to exchange %s with routing key %s&quot;</span>,
     q.Name, <span class="hljs-string">&quot;logs_direct&quot;</span>, s)
  err = ch.QueueBind(
    q.Name,        <span class="hljs-comment">// queue name</span>
    s,             <span class="hljs-comment">// routing key</span>
    <span class="hljs-string">&quot;logs_direct&quot;</span>, <span class="hljs-comment">// exchange</span>
    <span class="hljs-literal">false</span>,
    <span class="hljs-literal">nil</span>)
  failOnError(err, <span class="hljs-string">&quot;Failed to bind a queue&quot;</span>)
}
</code></pre>
<h2 id="&#x4EE3;&#x7801;&#x6574;&#x5408;">&#x4EE3;&#x7801;&#x6574;&#x5408;</h2>
<p><img src="http://www.rabbitmq.com/img/tutorials/python-four.png" alt=""></p>
<p>emit_log_direct.go&#x7684;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> (
        <span class="hljs-string">&quot;log&quot;</span>
        <span class="hljs-string">&quot;os&quot;</span>
        <span class="hljs-string">&quot;strings&quot;</span>

        <span class="hljs-string">&quot;github.com/streadway/amqp&quot;</span>
)

<span class="hljs-keyword">func</span> failOnError(err error, msg <span class="hljs-keyword">string</span>) {
        <span class="hljs-keyword">if</span> err != <span class="hljs-literal">nil</span> {
                log.Fatalf(<span class="hljs-string">&quot;%s: %s&quot;</span>, msg, err)
        }
}

<span class="hljs-keyword">func</span> main() {
        conn, err := amqp.Dial(<span class="hljs-string">&quot;amqp://guest:guest@localhost:5672/&quot;</span>)
        failOnError(err, <span class="hljs-string">&quot;Failed to connect to RabbitMQ&quot;</span>)
        <span class="hljs-keyword">defer</span> conn.Close()

        ch, err := conn.Channel()
        failOnError(err, <span class="hljs-string">&quot;Failed to open a channel&quot;</span>)
        <span class="hljs-keyword">defer</span> ch.Close()

        err = ch.ExchangeDeclare(
                <span class="hljs-string">&quot;logs_direct&quot;</span>, <span class="hljs-comment">// name</span>
                <span class="hljs-string">&quot;direct&quot;</span>,      <span class="hljs-comment">// type</span>
                <span class="hljs-literal">true</span>,          <span class="hljs-comment">// durable</span>
                <span class="hljs-literal">false</span>,         <span class="hljs-comment">// auto-deleted</span>
                <span class="hljs-literal">false</span>,         <span class="hljs-comment">// internal</span>
                <span class="hljs-literal">false</span>,         <span class="hljs-comment">// no-wait</span>
                <span class="hljs-literal">nil</span>,           <span class="hljs-comment">// arguments</span>
        )
        failOnError(err, <span class="hljs-string">&quot;Failed to declare an exchange&quot;</span>)

        body := bodyFrom(os.Args)
        err = ch.Publish(
                <span class="hljs-string">&quot;logs_direct&quot;</span>,         <span class="hljs-comment">// exchange</span>
                severityFrom(os.Args), <span class="hljs-comment">// routing key</span>
                <span class="hljs-literal">false</span>, <span class="hljs-comment">// mandatory</span>
                <span class="hljs-literal">false</span>, <span class="hljs-comment">// immediate</span>
                amqp.Publishing{
                        ContentType: <span class="hljs-string">&quot;text/plain&quot;</span>,
                        Body:        []<span class="hljs-keyword">byte</span>(body),
                })
        failOnError(err, <span class="hljs-string">&quot;Failed to publish a message&quot;</span>)

        log.Printf(<span class="hljs-string">&quot; [x] Sent %s&quot;</span>, body)
}

<span class="hljs-keyword">func</span> bodyFrom(args []<span class="hljs-keyword">string</span>) <span class="hljs-keyword">string</span> {
        <span class="hljs-keyword">var</span> s <span class="hljs-keyword">string</span>
        <span class="hljs-keyword">if</span> (<span class="hljs-built_in">len</span>(args) &lt; <span class="hljs-number">3</span>) || os.Args[<span class="hljs-number">2</span>] == <span class="hljs-string">&quot;&quot;</span> {
                s = <span class="hljs-string">&quot;hello&quot;</span>
        } <span class="hljs-keyword">else</span> {
                s = strings.Join(args[<span class="hljs-number">2</span>:], <span class="hljs-string">&quot; &quot;</span>)
        }
        <span class="hljs-keyword">return</span> s
}

<span class="hljs-keyword">func</span> severityFrom(args []<span class="hljs-keyword">string</span>) <span class="hljs-keyword">string</span> {
        <span class="hljs-keyword">var</span> s <span class="hljs-keyword">string</span>
        <span class="hljs-keyword">if</span> (<span class="hljs-built_in">len</span>(args) &lt; <span class="hljs-number">2</span>) || os.Args[<span class="hljs-number">1</span>] == <span class="hljs-string">&quot;&quot;</span> {
                s = <span class="hljs-string">&quot;info&quot;</span>
        } <span class="hljs-keyword">else</span> {
                s = os.Args[<span class="hljs-number">1</span>]
        }
        <span class="hljs-keyword">return</span> s
}
</code></pre>
<p>receive_logs_direct.go&#x7684;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> (
        <span class="hljs-string">&quot;log&quot;</span>
        <span class="hljs-string">&quot;os&quot;</span>

        <span class="hljs-string">&quot;github.com/streadway/amqp&quot;</span>
)

<span class="hljs-keyword">func</span> failOnError(err error, msg <span class="hljs-keyword">string</span>) {
        <span class="hljs-keyword">if</span> err != <span class="hljs-literal">nil</span> {
                log.Fatalf(<span class="hljs-string">&quot;%s: %s&quot;</span>, msg, err)
        }
}

<span class="hljs-keyword">func</span> main() {
        conn, err := amqp.Dial(<span class="hljs-string">&quot;amqp://guest:guest@localhost:5672/&quot;</span>)
        failOnError(err, <span class="hljs-string">&quot;Failed to connect to RabbitMQ&quot;</span>)
        <span class="hljs-keyword">defer</span> conn.Close()

        ch, err := conn.Channel()
        failOnError(err, <span class="hljs-string">&quot;Failed to open a channel&quot;</span>)
        <span class="hljs-keyword">defer</span> ch.Close()

        err = ch.ExchangeDeclare(
                <span class="hljs-string">&quot;logs_direct&quot;</span>, <span class="hljs-comment">// name</span>
                <span class="hljs-string">&quot;direct&quot;</span>,      <span class="hljs-comment">// type</span>
                <span class="hljs-literal">true</span>,          <span class="hljs-comment">// durable</span>
                <span class="hljs-literal">false</span>,         <span class="hljs-comment">// auto-deleted</span>
                <span class="hljs-literal">false</span>,         <span class="hljs-comment">// internal</span>
                <span class="hljs-literal">false</span>,         <span class="hljs-comment">// no-wait</span>
                <span class="hljs-literal">nil</span>,           <span class="hljs-comment">// arguments</span>
        )
        failOnError(err, <span class="hljs-string">&quot;Failed to declare an exchange&quot;</span>)

        q, err := ch.QueueDeclare(
                <span class="hljs-string">&quot;&quot;</span>,    <span class="hljs-comment">// name</span>
                <span class="hljs-literal">false</span>, <span class="hljs-comment">// durable</span>
                <span class="hljs-literal">false</span>, <span class="hljs-comment">// delete when usused</span>
                <span class="hljs-literal">true</span>,  <span class="hljs-comment">// exclusive</span>
                <span class="hljs-literal">false</span>, <span class="hljs-comment">// no-wait</span>
                <span class="hljs-literal">nil</span>,   <span class="hljs-comment">// arguments</span>
        )
        failOnError(err, <span class="hljs-string">&quot;Failed to declare a queue&quot;</span>)

        <span class="hljs-keyword">if</span> <span class="hljs-built_in">len</span>(os.Args) &lt; <span class="hljs-number">2</span> {
                log.Printf(<span class="hljs-string">&quot;Usage: %s [info] [warning] [error]&quot;</span>, os.Args[<span class="hljs-number">0</span>])
                os.Exit(<span class="hljs-number">0</span>)
        }
        <span class="hljs-keyword">for</span> _, s := <span class="hljs-keyword">range</span> os.Args[<span class="hljs-number">1</span>:] {
                log.Printf(<span class="hljs-string">&quot;Binding queue %s to exchange %s with routing key %s&quot;</span>,
                        q.Name, <span class="hljs-string">&quot;logs_direct&quot;</span>, s)
                err = ch.QueueBind(
                        q.Name,        <span class="hljs-comment">// queue name</span>
                        s,             <span class="hljs-comment">// routing key</span>
                        <span class="hljs-string">&quot;logs_direct&quot;</span>, <span class="hljs-comment">// exchange</span>
                        <span class="hljs-literal">false</span>,
                        <span class="hljs-literal">nil</span>)
                failOnError(err, <span class="hljs-string">&quot;Failed to bind a queue&quot;</span>)
        }

        msgs, err := ch.Consume(
                q.Name, <span class="hljs-comment">// queue</span>
                <span class="hljs-string">&quot;&quot;</span>,     <span class="hljs-comment">// consumer</span>
                <span class="hljs-literal">true</span>,   <span class="hljs-comment">// auto ack</span>
                <span class="hljs-literal">false</span>,  <span class="hljs-comment">// exclusive</span>
                <span class="hljs-literal">false</span>,  <span class="hljs-comment">// no local</span>
                <span class="hljs-literal">false</span>,  <span class="hljs-comment">// no wait</span>
                <span class="hljs-literal">nil</span>,    <span class="hljs-comment">// args</span>
        )
        failOnError(err, <span class="hljs-string">&quot;Failed to register a consumer&quot;</span>)

        forever := <span class="hljs-built_in">make</span>(<span class="hljs-keyword">chan</span> <span class="hljs-keyword">bool</span>)

        <span class="hljs-keyword">go</span> <span class="hljs-keyword">func</span>() {
                <span class="hljs-keyword">for</span> d := <span class="hljs-keyword">range</span> msgs {
                        log.Printf(<span class="hljs-string">&quot; [x] %s&quot;</span>, d.Body)
                }
        }()

        log.Printf(<span class="hljs-string">&quot; [*] Waiting for logs. To exit press CTRL+C&quot;</span>)
        &lt;-forever
}
</code></pre>
<p>&#x5982;&#x679C;&#x4F60;&#x5E0C;&#x671B;&#x53EA;&#x662F;&#x4FDD;&#x5B58;warning&#x548C;error&#x7EA7;&#x522B;&#x7684;&#x65E5;&#x5FD7;&#x5230;&#x78C1;&#x76D8;&#xFF0C;&#x53EA;&#x9700;&#x8981;&#x6253;&#x5F00;&#x63A7;&#x5236;&#x53F0;&#x5E76;&#x8F93;&#x5165;&#xFF1A;</p>
<pre><code>$ go run receive_logs_direct.go warning error &gt; logs_from_rabbit.log
</code></pre><p>&#x5982;&#x679C;&#x4F60;&#x5E0C;&#x671B;&#x6240;&#x6709;&#x7684;&#x65E5;&#x5FD7;&#x4FE1;&#x606F;&#x90FD;&#x8F93;&#x51FA;&#x5230;&#x5C4F;&#x5E55;&#x4E2D;&#xFF0C;&#x6253;&#x5F00;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x7EC8;&#x7AEF;&#xFF0C;&#x7136;&#x540E;&#x8F93;&#x5165;&#xFF1A;</p>
<pre><code>go run receive_logs_direct.go info warning error
# =&gt; [*] Waiting for logs. To exit press CTRL+C
</code></pre><p>&#x5982;&#x679C;&#x8981;&#x89E6;&#x53D1;&#x4E00;&#x4E2A;error&#x7EA7;&#x522B;&#x7684;&#x65E5;&#x5FD7;&#xFF0C;&#x53EA;&#x9700;&#x8981;&#x8F93;&#x5165;&#xFF1A;</p>
<pre><code>go run emit_log_direct.go error &quot;Run. Run. Or it will explode.&quot;
# =&gt; [x] Sent &apos;error&apos;:&apos;Run. Run. Or it will explode.&apos;
</code></pre><p>&#x8FD9;&#x91CC;&#x662F;&#x5B8C;&#x6574;&#x7684;&#x4EE3;&#x7801;&#xFF1A;(<a href="https://github.com/rabbitmq/rabbitmq-tutorials/blob/master/go/emit_log_direct.go" _target="blank">emit_log_direct.go</a>&#x548C;<a href="https://github.com/rabbitmq/rabbitmq-tutorials/blob/master/go/receive_logs_direct.go" _target="blank">receive_logs_direct.go</a>)</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="[3]Publish_Subscribe.html" class="navigation navigation-prev " aria-label="Previous page: 发布/订阅">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="[5]Topics.html" class="navigation navigation-next " aria-label="Next page: 主题交换机">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"路由","level":"1.5.3.4","depth":3,"next":{"title":"主题交换机","level":"1.5.3.5","depth":3,"path":"tutorials_with_golang/[5]Topics.md","ref":"tutorials_with_golang/[5]Topics.md","articles":[]},"previous":{"title":"发布/订阅","level":"1.5.3.3","depth":3,"path":"tutorials_with_golang/[3]Publish_Subscribe.md","ref":"tutorials_with_golang/[3]Publish_Subscribe.md","articles":[]},"dir":"ltr"},"config":{"plugins":["ga","expandable-chapters-interactive","-lunr","-search","search-plus"],"root":"./published","styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"ga":{"token":"UA-120619814-1","configuration":"auto"},"expandable-chapters-interactive":{},"search-plus":{},"highlight":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"Ping <yuanta11@gmil.com>","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"RabbitMQ in Chinese","links":{"sharing":{"all":null,"facebook":null,"google":null,"twitter":null,"weibo":null},"sidebar":{"瓶先生":"http://mr-ping.com"}},"gitbook":"=3.0.0-pre.14","description":"RabbitMQ in Chinese"},"file":{"path":"tutorials_with_golang/[4]Routing.md","mtime":"2020-10-30T03:40:00.900Z","type":"markdown"},"gitbook":{"version":"3.0.0-pre.14","time":"2020-10-30T03:40:06.094Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-ga/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-expandable-chapters-interactive/expandable-chapters.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search-plus/jquery.mark.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search-plus/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/buttons.js"></script>
        
    

    </body>
</html>

